package com.stone.business.weekplan.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.stone.business.weekplan.pojo.TWeekplan;
import com.stone.business.weekplan.service.WeekplanService;
import com.stone.common.pojo.TFile;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.DateFormatTool;
import com.stone.utils.FileUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class WeekplanAction extends BaseAction {
	private static final long serialVersionUID = -4160904495100767139L;
	private WeekplanService weekplanService;
	private TWeekplan entity;
	private String id;
	
	private File myFile1;
	private String myFile1FileName;
	
	private List<TFile> xsbFileList;

	public WeekplanService getWeekplanService() {
		return weekplanService;
	}

	public void setWeekplanService(WeekplanService weekplanService) {
		this.weekplanService = weekplanService;
	}

	public TWeekplan getEntity() {
		return entity;
	}

	public void setEntity(TWeekplan entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File getMyFile1() {
		return myFile1;
	}

	public void setMyFile1(File myFile1) {
		this.myFile1 = myFile1;
	}

	public String getMyFile1FileName() {
		return myFile1FileName;
	}

	public void setMyFile1FileName(String myFile1FileName) {
		this.myFile1FileName = myFile1FileName;
	}

	public List<TFile> getXsbFileList() {
		return xsbFileList;
	}

	public void setXsbFileList(List<TFile> xsbFileList) {
		this.xsbFileList = xsbFileList;
	}
	
	public String initWeekPlanDetail() throws AppException{
		// 修改进入
		if (!"0".equals(id)) {
			this.entity = (TWeekplan) weekplanService.findById(Long.parseLong(id),
					TWeekplan.class.getName());
			getFileList();
			// 新建进入
		} else {
			this.entity = new TWeekplan();
			String sysdate = weekplanService.getSysdate("yyyy-MM-dd");
			int weeknum = DateFormatTool.weekOfMonth(DateFormatTool.formatToDate(sysdate, false), false);
			this.entity.setTbrq(DateFormatTool.formatToDate(sysdate, false));
			TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
			String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
			TOrgan og = (TOrgan)(super.getSession().getAttribute(IConstant.SESSION_TORGAN));
			this.entity.setTbr(loginUser.getName());
			this.entity.setTbrq(DateFormatTool.formatToDate(sysdate, false));
			this.entity.setOrganid(organCode);
			this.entity.setOrganname(og.getOrganName());
			this.entity.setNy(sysdate.substring(0, 7));
			this.entity.setWeeknum(weeknum + "");
		}
		
		return SUCCESS;
	}
	
	public String addWeekplan() throws AppException{
		if (myFile1 != null && (myFile1.length() > IConstant.FILE_BUFFER_SIZE
						* IConstant.FILE_SIZE_TEN * IConstant.FILE_SIZE_HUNDRED)) {
			super.setInfo("上传失败，附件不能超过10M");//OUTOFSIZE
			return SUCCESS;
		}
		ArrayList<TFile> fileList = new ArrayList<TFile>();
		TFile tFile = null;
		String filePath = super.getRealPath() + "upload/weekplan";
		//检查是否之前上传过当前周的计划, 如果有的话，先删除历史文件
		String sql = "from TWeekplan where ny = '" + this.entity.getNy() + "' AND weeknum = '"+entity.getWeeknum()+"' AND organid='"+entity.getOrganid()+"'";
		TWeekplan tw = (TWeekplan)weekplanService.findFirst(sql);
		if(tw != null){
			this.entity.setId(tw.getId());
			
			String sql2 = "from TFile where tableid = '" + tw.getId() + "' AND colname = 'TAB_WEEKPLAN' AND filename='周计划'";
			TFile tmpFile = (TFile)weekplanService.findFirst(sql2);
			if(tmpFile != null){
				File f = new File(filePath + "\\" + tmpFile.getRealfilename());
				if(f.exists())
					f.delete();
				
				weekplanService.delete(tmpFile.getId(), TFile.class.getName());
			}
		}
		
		if(myFile1!=null){
			System.out.println("==========" + myFile1FileName);
            
            File upDir = new File(filePath);
            if (!upDir.exists())
            {
                upDir.mkdirs();
            }
            
            String saveFileName = UUID.randomUUID().toString() + FileUtils.getExtention(myFile1FileName);
            File saveFile = new File(filePath + "\\" +saveFileName);
         // 上传文件
			try {
				org.apache.commons.io.FileUtils.copyFile(myFile1, saveFile);
				tFile = new TFile();
				tFile.setUrl("/DLPLANMIS/upload/weekplan/" + saveFileName);
				tFile.setFilename("周计划");
				tFile.setRealfilename(saveFileName);
				
				tFile.setTableid(entity.getId());
				tFile.setColname("TAB_WEEKPLAN");
				tFile.setOperuser(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				tFile.setOperdate(DateFormatTool.formatToDate(weekplanService
						.getSysdate("yyyy-MM-dd HH24:mi"), true));
				tFile.setImageflag("0");
				
				fileList.add(tFile);
			} catch (IOException e) {
				e.printStackTrace();
				super.setInfo("文件上传出现错误");
				throw new AppException("上传失败");
			}
		}
		String rtn = weekplanService.doSave(entity,  fileList);
		super.setInfo(rtn);
		this.setId(entity.getId() + "");
		getFileList();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public void getFileList() throws AppException {
		xsbFileList = weekplanService.findListByHql("from TFile where tableid = '" + id + "' AND colname = 'TAB_WEEKPLAN'");
	}
}
